package com.jiantong.web.open.app.work;

import com.jiantong.common.util.UserUtil;
import com.jiantong.user.ext.LoginUser;
import com.jiantong.work.model.ButcherReportsForm;
import com.jiantong.work.service.ButcherReportsFormReadService;
import com.jiantong.work.service.HarmlessReadService;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import io.terminus.pampas.openplatform.annotations.OpenBean;
import io.terminus.pampas.openplatform.annotations.OpenMethod;
import io.terminus.pampas.openplatform.exceptions.OPServerException;
import lombok.extern.slf4j.Slf4j;
import org.assertj.core.util.Strings;
import org.joda.time.DateTime;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by niufei on 2017/4/17.
 * 屠宰检疫报表
 */
@Slf4j
@OpenBean
public class ButcherReportsFormApis {

    @RpcConsumer
    private ButcherReportsFormReadService butcherReportsFormReadService;

    @RpcConsumer
    private HarmlessReadService harmlessReadService;

    @OpenMethod(
            key = "find.butcher.harmless.reports.from.by.id",
            paramNames = {"id"},
            httpMethods = RequestMethod.GET
    )
    public ButcherReportsForm findButcherHarmlessReportsFromById(Long id){
        Response<ButcherReportsForm> response = butcherReportsFormReadService.findButcherReportsFormById(id);
        if(!response.isSuccess()){
            log.error("failed to find butcher report form by id = {}, cause : {}", id,response.getError());
            throw new OPServerException(response.getError());
        }
        ButcherReportsForm butcherReportsForm = response.getResult();
        if(butcherReportsForm.getAdmissionHarmlessId() != null){
            Response<Map<String , Integer>> response1 = harmlessReadService.findStatusAndTreatment(butcherReportsForm.getAdmissionHarmlessId());
            if(!response1.isSuccess()) {
                log.error("failed to find StatusAndTreatment by id = {}, cause : {}", butcherReportsForm.getAdmissionHarmlessId(),response1.getError());
                throw new OPServerException(response1.getError());
            }
            butcherReportsForm.setAdmissionHarmlessStatus(response1.getResult().get("status"));
            butcherReportsForm.setAdmissionHarmlessTreatment(response1.getResult().get("treatment"));
        }
        if(butcherReportsForm.getBeforeSlaughterHarmlessId() != null){
            Response<Map<String , Integer>> response1 = harmlessReadService.findStatusAndTreatment(butcherReportsForm.getBeforeSlaughterHarmlessId());
            if(!response1.isSuccess()) {
                log.error("failed to find StatusAndTreatment by id = {}, cause : {}", butcherReportsForm.getBeforeSlaughterHarmlessId(),response1.getError());
                throw new OPServerException(response1.getError());
            }
            butcherReportsForm.setBeforeSlaughterHarmlessStatus(response1.getResult().get("status"));
            butcherReportsForm.setBeforeSlaughterHarmlessTreatment(response1.getResult().get("treatment"));
        }

        if(butcherReportsForm.getSyncSlaughterHarmlessId() != null){
            Response<Map<String , Integer>> response1 = harmlessReadService.findStatusAndTreatment(butcherReportsForm.getSyncSlaughterHarmlessId());
            if(!response1.isSuccess()) {
                log.error("failed to find StatusAndTreatment by id = {}, cause : {}", butcherReportsForm.getSyncSlaughterHarmlessId(),response1.getError());
                throw new OPServerException(response1.getError());
            }
            butcherReportsForm.setSyncSlaughterHarmlessStatus(response1.getResult().get("status"));
            butcherReportsForm.setSyncSlaughterHarmlessTreatment(response1.getResult().get("treatment"));
        }

        return butcherReportsForm;
    }

    @OpenMethod(
            key = "find.butcher.reports.form.by.id",
            paramNames = {"id"},
            httpMethods = RequestMethod.GET
    )
    public ButcherReportsForm findButcherReportsFormById(Long id){
        Response<ButcherReportsForm> response = butcherReportsFormReadService.findButcherReportsFormById(id);
        if(!response.isSuccess()){
            log.error("failed to find butcher report form by id = {}, cause : {}", id,response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }

    @OpenMethod(
            key = "find.sum.group.by.product.type",
            paramNames = {"fromDate","toDate","butcherFactoryIds"},
            httpMethods = RequestMethod.GET
    )
    public List<Map<String , Object>> findSumGroupByProductType(String fromDate , String toDate , List<Long> butcherFactoryIds){
        LoginUser loginUser = UserUtil.getCurrentUser();
        Integer organizationType = loginUser.getOrganizationType();
        if(organizationType == 3) {
            Integer code = (Integer) loginUser.getRoles().get("code");
            if (code == 3) {
                if(butcherFactoryIds.size() != 1){
                    butcherFactoryIds = null;
                }
            }
        }
        Date start = null;
        if(!Strings.isNullOrEmpty(fromDate)){
            start = DateTime.parse(fromDate).toDate();
        }
        Date end = null;
        if(!Strings.isNullOrEmpty(toDate)){
            end = DateTime.parse(toDate).toDate();
        }
        Response<List<Map<String ,Object>>> response = butcherReportsFormReadService.findSumGroupByProductType(start,end,butcherFactoryIds);
        if(!response.isSuccess()){
            log.error("failed to find sum butcherFactoryIds = {},case = {}",butcherFactoryIds,response.getError());
            throw new OPServerException(response.getError());
        }

        return response.getResult();
    }

    @OpenMethod(
            key = "paging.butcher.reports.form",
            paramNames = {"pageNo","pageSize","fromDate","toDate","butcherFactoryIds","productType"},
            httpMethods = RequestMethod.GET
    )
    public Paging<ButcherReportsForm> butcherReportsFormPaging(Integer pageNo, Integer pageSize , String fromDate , String toDate , List<Long> butcherFactoryIds , Integer productType){
        LoginUser loginUser = UserUtil.getCurrentUser();
        Integer organizationType = loginUser.getOrganizationType();
        if(organizationType == 3) {
            Integer code = (Integer) loginUser.getRoles().get("code");
            if (code == 3) {
                if(butcherFactoryIds.size() != 1){
                    butcherFactoryIds = null;
                }
            }
        }
        Date start = null;
        if(!Strings.isNullOrEmpty(fromDate)){
            start = DateTime.parse(fromDate).toDate();
        }
        Date end = null;
        if(!Strings.isNullOrEmpty(toDate)){
            end = DateTime.parse(toDate).toDate();
        }
        Response<Paging<ButcherReportsForm>> response = butcherReportsFormReadService.pingButcherReportForm(pageNo,pageSize,start,end,butcherFactoryIds,productType);
        if(!response.isSuccess()){
            log.error("failed to paging ButcherReportsForm butcherFactoryIds = {}, productType = {},case = {}",butcherFactoryIds,productType,response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }


    @OpenMethod(
            key = "find.harmless.sum.group.by.product.type",
            paramNames = {"fromDate","toDate","butcherFactoryIds"},
            httpMethods = RequestMethod.GET
    )
    public List<Map<String , Object>> findHarmlessSumGroupByProductType(String fromDate , String toDate , List<Long> butcherFactoryIds){
        LoginUser loginUser = UserUtil.getCurrentUser();
        Integer organizationType = loginUser.getOrganizationType();
        if(organizationType == 3) {
            Integer code = (Integer) loginUser.getRoles().get("code");
            if (code == 3) {
                if(butcherFactoryIds.size() != 1){
                    butcherFactoryIds = null;
                }
            }
        }

        Date start = null;
        if(!Strings.isNullOrEmpty(fromDate)){
            start = DateTime.parse(fromDate).toDate();
        }
        Date end = null;
        if(!Strings.isNullOrEmpty(toDate)){
            end = DateTime.parse(toDate).toDate();
        }
        Response<List<Map<String ,Object>>> response = butcherReportsFormReadService.findHarmlessSumGroupByProductType(start,end,butcherFactoryIds);
        if(!response.isSuccess()){
            log.error("failed to find harmless sum butcherFactoryIds = {},case = {}",butcherFactoryIds,response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }

    @OpenMethod(
            key = "paging.harmless.butcher,reports.form",
            paramNames = {"pageNo","pageSize","fromDate","toDate","butcherFactoryIds","productType"},
            httpMethods = RequestMethod.GET
    )
    public Paging<ButcherReportsForm> pagingButcherHarmlessForm(Integer pageNo, Integer pageSize , String fromDate , String toDate , List<Long> butcherFactoryIds , Integer productType){
        LoginUser loginUser = UserUtil.getCurrentUser();
        Integer organizationType = loginUser.getOrganizationType();
        if(organizationType == 3) {
            Integer code = (Integer) loginUser.getRoles().get("code");
            if (code == 3) {
                if(butcherFactoryIds.size() != 1){
                    butcherFactoryIds = null;
                }
            }
        }

        Date start = null;
        if(!Strings.isNullOrEmpty(fromDate)){
            start = DateTime.parse(fromDate).toDate();
        }
        Date end = null;
        if(!Strings.isNullOrEmpty(toDate)){
            end = DateTime.parse(toDate).toDate();
        }
        Response<Paging<ButcherReportsForm>> response = butcherReportsFormReadService.pagingButcherHarmlessForm(pageNo,pageSize,start,end,butcherFactoryIds,productType);
        if(!response.isSuccess()){
            log.error("failed to paging harmless ButcherReportsForm butcherFactoryIds = {}, productType = {},case = {}",butcherFactoryIds,productType,response.getError());
            throw new OPServerException(response.getError());
        }
        Paging<ButcherReportsForm> paging = response.getResult();
        List<ButcherReportsForm> list = paging.getData();
        for(ButcherReportsForm butcherReportsForm : list){
            Long admissionHarmlessId = butcherReportsForm.getAdmissionHarmlessId();
            Long beforeSlaughterHarmlessId = butcherReportsForm.getBeforeSlaughterHarmlessId();
            Long syncSlaughterHarmlessId = butcherReportsForm.getSyncSlaughterHarmlessId();
            if(admissionHarmlessId != null){
                Map<String , Integer> admissionHarmlessMap = harmlessReadService.findStatusAndTreatment(admissionHarmlessId).getResult();
                if(admissionHarmlessMap != null){
                    butcherReportsForm.setAdmissionHarmlessTreatment(admissionHarmlessMap.get("treatment"));
                    butcherReportsForm.setAdmissionHarmlessStatus(admissionHarmlessMap.get("status"));
                }
            }
            if(beforeSlaughterHarmlessId != null){
                Map<String , Integer> beforeSlaughterHarmlessMap = harmlessReadService.findStatusAndTreatment(beforeSlaughterHarmlessId).getResult();
                if(beforeSlaughterHarmlessMap!= null) {
                    butcherReportsForm.setBeforeSlaughterHarmlessTreatment(beforeSlaughterHarmlessMap.get("treatment"));
                    butcherReportsForm.setBeforeSlaughterHarmlessStatus(beforeSlaughterHarmlessMap.get("status"));
                }
            }
            if(syncSlaughterHarmlessId != null){
                Map<String , Integer> syncSlaughterHarmlessMap = harmlessReadService.findStatusAndTreatment(syncSlaughterHarmlessId).getResult();
                if(syncSlaughterHarmlessMap != null) {
                    butcherReportsForm.setSyncSlaughterHarmlessTreatment(syncSlaughterHarmlessMap.get("treatment"));
                    butcherReportsForm.setSyncSlaughterHarmlessStatus(syncSlaughterHarmlessMap.get("status"));
                }
            }

        }
        paging.setData(list);
        return paging;
    }
}
